<?php

/* SLOVNIKY */



/* =================================
 * Generování boxu (menu)
 * ================================= */
function slovnikMenuBox() {
    global $config;
    $tmpBoxObsah = '
    <div>
        <a href="'.$config["cesta"]["url"].'slovniky.htm">Slovníky</a>
        '.maUzivatelOpravneni('<a href="'.$config["cesta"]["url"].'slovniky/novySlovnik.htm">Nový slovník</a>').'
    </div>
    ';
    
    return box('Akce',$tmpBoxObsah);
}

/* =================================
 * Formular pro upravu/vytvoreni slovniku
 * ================================= */
function slovnikForm() {
    global $_POST;
    global $kontrolaVyplneni;
    global $row;
    
    if(!empty($_POST)) {
        $data = $_POST;
    } else {
        $data = $row;
    }

    $output .= tableTitle('Slovník');
    $output .= tableData('Název','<input type="text" name="nazevSlovniku"'.kontrolaVyplneniVstup("nazevSlovniku",$kontrolaVyplneni[1]).' value="'.$data["nazevSlovniku"].'" maxlength="100" size="80">');
    $output .= tableData('Poznámka','<textarea name="poznamka" cols="50" rows="10">'.$data["poznamka"].'</textarea>');
    
    return $output;
}

/* =================================
 * Formular pro upravu/vytvoreni slova
 * ================================= */
function slovoForm() {
    global $_POST;
    global $kontrolaVyplneni;
    global $row;
    
    if(!empty($_POST)) {
        $data = $_POST;
    } else {
        $data = $row;
    }

    $output .= tableTitle('Slovo');
    $output .= tableData('Zkratka slova','<input type="text" name="zkratkaSlova"'.kontrolaVyplneniVstup("zkratkaSlova",$kontrolaVyplneni[1]).' value="'.$data["zkratkaSlova"].'" maxlength="10" size="12">'); 
    $output .= tableData('Slovo','<input type="text" name="slovo"'.kontrolaVyplneniVstup("slovo",$kontrolaVyplneni[1]).' value="'.$data["slovo"].'" maxlength="255" size="80">');
    
    return $output;
}

/* =================================
 * Pocet slov ve slovniku
 * ================================= */
function pocetSlovSlovniku($idSlovniku) {
    global $db;
    
    vytvorDbSlovnik($idSlovniku);
    
    $query = "
        SELECT count(*) as pocet
        FROM `slovnik_".$idSlovniku."`
    ";
    $row = $db->GetRow($query);
    return($row["pocet"]);
}


/* =================================
 * Vytvoreni noveho slovniku
 * ================================= */
function vytvorDbSlovnik($tabulkaSlovniku) {
    global $db;
//Pokud se jedna o novy slovnik, vytvorime v db tabulku
        $query = "
             CREATE TABLE IF NOT EXISTS `slovnik_".$tabulkaSlovniku."` (
                `idSlova` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                slovo VARCHAR( 255 ) NULL ,
                zkratkaSlova VARCHAR(10)  NULL,
                poznamka TEXT  NULL
            )
        ";
        $db->Execute($query);
        
        
        $query = "
            CREATE TABLE IF NOT EXISTS `nm_predmet2slovo_".$tabulkaSlovniku."` (
                idSlova VARCHAR(11) NOT NULL,
                idPredmetu INT(11) NOT NULL,
                uzivatelskaHodnota VARCHAR(255) NULL,
                PRIMARY KEY ( `idPredmetu` , `idSlova` )
            )
        ";
        $db->Execute($query);
}

/* =================================
 * Generovani query pro vypis slov systemoveho slovniku
 * ================================= */
function selectQuerySlovnik($tabulkaSlovniku) {
    switch($tabulkaSlovniku) {
        case "skFunkce":    $query = "
                                    SELECT 
                                            idSkFunkce AS id,
                                            nazevSkFunkce AS nazev
                                    FROM
                                            slovnik_skfunkce
                                    ORDER BY
                                            nazev ASC
                                ";
            break;
        case "skMaterial":    $query = "
                                    SELECT 
                                            idSkMaterial AS id,
                                            nazevSkMaterial AS nazev
                                    FROM
                                            slovnik_skmaterial
                                    
                                ";
            break;
        case "datace":    $query = "
                                    SELECT 
                                            zkratkaDatace AS id,
                                            zkratkaDatace AS zkratkaSlova,
                                            CONCAT(nazevDatace,' ',upresnPoznamka) AS nazev
                                    FROM
                                            slovnik_datace
                                    ORDER BY
                                            poradi ASC
                                    
                                ";
            break;
        case "lokace":    $query = "
                                    SELECT 
                                            idLokace AS id,
                                            nazevLokace AS nazev
                                    FROM
                                            slovnik_lokace
                                    ORDER BY
                                            nazevLokace ASC
                                    
                                ";
        break;
        case "katastry":    $query = "
                                    SELECT DISTINCT
                                            CONCAT(nazevCastiObce, ' (okres ',nazevOkresu,')') AS nazev,
                                            kodCastiObce AS id
                                            
                                    FROM
                                            slovnik_katastry
                                    ORDER BY
                                            nazevCastiObce ASC,
                                            nazevOkresu ASC
                                    
                                ";
            break;
        case "subjekt":    $query = "
                                    SELECT 
                                            idSubjektu AS id,
                                            nazevSubjektu AS nazev
                                    FROM
                                            adr_adresar
                                    ORDER BY
                                            nazevSubjektu ASC
                                    
                                ";
            break;
        case "zpusobNabyti" OR "zpusobnabyti":    $query = "
                                    SELECT 
                                            idZpusobuNabyti AS id,
                                            nazevZpusobuNabyti AS nazev
                                    FROM
                                            slovnik_zpusobnabyti
                                    ORDER BY
                                            nazevZpusobuNabyti ASC
                                    
                                ";
        break;
        default:
                                $query = "
                                    SELECT
                                            idSlova as id,
                                            slovo as nazev,
                                            zkratkaSlova
                                    FROM
                                            `slovnik_".$tabulkaSlovniku."`
                                    ORDER BY
                                            slovo ASC
                                ";
        break;
}

    return $query;
}


function selectQuerySlovnik2Predmet($tabulkaSlovniku, $idPredmetu) {
    switch($tabulkaSlovniku) {
        case "datace":        $query = "
                                    SELECT
                                    slovnik_datace.nazevDatace as slovo,
                                    slovnik_datace.zkratkaDatace as zkratkaSlova,
                                    uzivatelskaHodnota
                                    FROM
                                    slovnik_datace
                                    Right Join nm_predmet2slovo_datace ON slovnik_datace.zkratkaDatace = nm_predmet2slovo_datace.idSlova
                                    WHERE
                                    nm_predmet2slovo_datace.idPredmetu =  '".$idPredmetu."'
                                ";
                            
            break;
        case "lokace":        $query = "
                                    SELECT
                                            slovnik_lokace.nazevLokace as slovo,
                                            uzivatelskaHodnota
                                    FROM
                                            nm_predmet2slovo_lokace
                                            Left Join slovnik_lokace ON slovnik_lokace.idLokace = nm_predmet2slovo_lokace.idSlova
                                    WHERE
                                            nm_predmet2slovo_lokace.idPredmetu =  '".$idPredmetu."'
                                ";
                            
            break;
        default:
            $query = "
                        SELECT
                                slovnik_".$tabulkaSlovniku.".slovo,
                                slovnik_".$tabulkaSlovniku.".poznamka,
                                uzivatelskaHodnota
                        FROM
                                nm_predmet2slovo_".$tabulkaSlovniku."
                                Left Join slovnik_".$tabulkaSlovniku." ON slovnik_".$tabulkaSlovniku.".idSlova = nm_predmet2slovo_".$tabulkaSlovniku.".idSlova
                        WHERE
                                nm_predmet2slovo_".$tabulkaSlovniku.".idPredmetu =  '".$idPredmetu."'
            ";
            break;
    }
    
    return $query;
}

?>